package com.firstgroup.service.Interceptors;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.firstgroup.Utils.JwtUtil;
import com.firstgroup.domain.User;
import com.firstgroup.service.Impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 创作人 王箫凯
 * Date 2021/8/9
 * Time 17:11
 *
 * @author 王箫凯
 */
@Slf4j
public class adminInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("UTF-8");
        //对管理员进行验证
        String token=request.getHeader("token");
        if (token == null) {
            log.info("不存在token，请进行登录");
            response.sendRedirect(request.getContextPath()+"/InterceptorResult/NotLogin");
            return false;
        }
        DecodedJWT decodedJWT = JwtUtil.jwtInf(token);
        String username=decodedJWT.getClaim("username").asString();
        if (! "wlgzs".equals(username)){
            response.sendRedirect(request.getContextPath()+"/InterceptorResult/NoPower");
            log.info("您没有该权限");
            return false;
        }
        return true;
    }
}
